package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.scyx.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.scyx.ProcessOperationMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.scyx.ProcessOperation;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.scyx.ProcessOperationService;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Entity;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EntityTypePro;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Package com.cqhilink.iot.integratedapplicationmanagement.services.innerservice.scyx.impl
 * @Description: ${TODO}(工艺运行实现)
 * @author: lutianbo
 * @date: 2017/2/6,0006
 */
public class ProcessOperationServiceImpl implements ProcessOperationService {
    @Autowired
    private ProcessOperationMapper operationMapper;


    /**
     * 根据生产实体类型查询工艺运行设备特性
     * @param processOperation
     * @return
     */
    @Override
    public JSONObject queryListByEntityTypeId(ProcessOperation processOperation) {
        JSONObject jsonObject = new JSONObject();
        processOperation.setLimit( processOperation.getLimit() == null ? 10 : processOperation.getLimit() );
        processOperation.setOffset( processOperation.getOffset() == null ? 0 : processOperation.getOffset() );
        if(processOperation.getEntityTypeId()==null||processOperation.getEntityTypeId().equals("")){
            return jsonObject;
        }
        // 确定表头 根据生产实体类型查询
        List<EntityTypePro> entityTypeProList = operationMapper.selectByEnTypeId(processOperation.getEntityTypeId());
        // 确定行数据
        List<Entity> entityList = operationMapper.selectEntityByEnTypeId(processOperation);
        // 生产实体特性id
        String entityTypeProId;
        // 生产实体id
        String entityId;
        Map<String,String> retMap;
        // 查询参数集合
        Map<String,Object> searchMap;
        // 数据集
        List<Map<String,String>> operationList = new ArrayList<>();
        // 数据封装
        for(Entity entity: entityList){
            entityId = entity.getEnId();
            retMap = new HashMap<>();
            for(EntityTypePro entityTypePro: entityTypeProList){
                entityTypeProId = entityTypePro.getDevicePropertyId();
                // 查找特征值
                searchMap = new HashMap<>();
                searchMap.put("entityId",entityId);
                searchMap.put("entityTypeProId",entityTypeProId);
                String value =  operationMapper.selectValueByIds(searchMap);
                retMap.put(entityTypeProId,value);
            }
            // 第一列数据
            retMap.put("entityId",entity.getEnName());
            operationList.add(retMap);
        }
        jsonObject.put("total",operationMapper.selectCount(processOperation));
        System.out.println("ff"+JSON.toJSON(operationList));
        jsonObject.put("rows",JSON.toJSON(operationList));
        return jsonObject;
    }
    /**
     * 根据生产实体类型设置表头
     * @param processOperation
     * @return
     */
    @Override
    public Object queryTitleList(ProcessOperation processOperation){
        if(processOperation.getEntityTypeId()==null||processOperation.getEntityTypeId().equals("")){
            return "-1";
        }
        // 确定表头 根据生产实体类型查询
        List<EntityTypePro> entityTypeProList = operationMapper.selectByEnTypeId(processOperation.getEntityTypeId());
        List< Map<String,String>> list = new ArrayList<>();
        Map<String,String> map = new HashMap<>();

        // 添加第一个表头
        map.put("title","生产实体");
        map.put("field","entityId");
        list.add(map);
        // 添加表头（第一个除外）
        for( EntityTypePro entityTypePro : entityTypeProList){
            map = new HashMap<>();
            map.put("title",entityTypePro.getPropertyName());
            map.put("field",entityTypePro.getDevicePropertyId());
            list.add(map);
        }
        System.out.println("titls" + JSON.toJSON(list));
        return JSON.toJSON(list);
    }

    @Override
    public JSONObject queryListByEntityTypeIds(ProcessOperation processOperation) {
        JSONObject jsonObject = new JSONObject();
        processOperation.setLimit( processOperation.getLimit() == null ? 10 : processOperation.getLimit() );
        processOperation.setOffset( processOperation.getOffset() == null ? 0 : processOperation.getOffset() );
        if(processOperation.getEntityTypeId()==null||processOperation.getEntityTypeId().equals("")){
            return jsonObject;
        }
        // 确定表头 根据生产实体类型查询
        List<EntityTypePro> entityTypeProList = operationMapper.selectByEnTypeId(processOperation.getEntityTypeId());
        // 确定行数据
        List<Entity> entityList = operationMapper.selectEntityByEnTypeId(processOperation);
        // 生产实体特性id
        String entityTypeProId;
        // 生产实体id
        String entityId;
        Map<String,String> retMap;
        // 查询参数集合
        Map<String,Object> searchMap;
        // 数据集
        List<Map<String,String>> operationList = new ArrayList<>();
        // 数据封装
        for(Entity entity: entityList){
            entityId = entity.getEnId();
            retMap = new HashMap<>();
            for(EntityTypePro entityTypePro: entityTypeProList){
                entityTypeProId = entityTypePro.getDevicePropertyId();
                // 查找特征值
                searchMap = new HashMap<>();
                searchMap.put("entityId",entityId);
                searchMap.put("entityTypeProId",entityTypeProId);
                String value =  operationMapper.selectValueByIds(searchMap);
                retMap.put(entityTypeProId,value);
            }
            // 第一列数据
            retMap.put("entityId",entity.getEnName());
            operationList.add(retMap);
        }
        jsonObject.put("total",operationMapper.selectCount(processOperation));
        jsonObject.put("rows",JSON.toJSON(operationList));
        return jsonObject;
    }

    @Override
    public Object queryTitleLists(ProcessOperation processOperation) {
        if(processOperation.getEntityTypeId()==null||processOperation.getEntityTypeId().equals("")){
            return "-1";
        }
        // 确定表头 根据生产实体类型查询
        List<EntityTypePro> entityTypeProList = operationMapper.selectByPropertyId(processOperation.getDeviceSumTempId());
        List< Map<String,String>> list = new ArrayList<>();
        Map<String,String> map = new HashMap<>();

        // 添加第一个表头
        map.put("title","生产实体");
        map.put("field","entityId");
        list.add(map);
        // 添加表头（第一个除外）
        for( EntityTypePro entityTypePro : entityTypeProList){
            map = new HashMap<>();
            map.put("title",entityTypePro.getPropertyName());
            map.put("field",entityTypePro.getDevicePropertyId());
            list.add(map);
        }

        return JSON.toJSON(list);
    }
}
